package io.renren.modules.xiaoqu.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.xiaoqu.dao.HouseDao;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.xiaoqu.entity.HouseEntity;
import io.renren.modules.xiaoqu.service.HouseService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 *
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2022-05-29 15:49:13
 */
@RestController
@RequestMapping("/xiaoqu/house")
public class HouseController {
    @Autowired
    private HouseService houseService;

    @Autowired
    private HouseDao houseDao;

    /**
     * 列表
     */
    @PostMapping("/list")
    public R list(@RequestBody Map<String, Object> params){
        params.replace("page",params.get("page").toString());
        params.replace("limit",params.get("limit").toString());

        PageUtils page = houseService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 查询（用户名）
     */
    @PostMapping("/info")
    public R info(int page,int pageSize,String username){

        if(username == null){
            Page<HouseEntity> pageInfo=new Page<>(page,pageSize);
            Page<HouseEntity> houseEntityPage = houseService.page(pageInfo);
            return R.ok().put("house", houseEntityPage);
        }

        LambdaQueryWrapper<HouseEntity> queryWrapper=new QueryWrapper<HouseEntity>().lambda()
                .like(HouseEntity::getHomename,username);

        Page<HouseEntity> pageInfo=new Page<>(page,pageSize);

        Page<HouseEntity> houseEntityPage = houseService.page(pageInfo, queryWrapper);

        return R.ok().put("house",houseEntityPage);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody HouseEntity house){
		houseService.save(house);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody HouseEntity house){
		houseService.updateById(house);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping ("/delete")
    public R delete(@RequestBody Integer[] ids){

        houseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
